define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));

                console.log(panel.size());
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'medicine/medicine/table1'+ location.search,
                    extend: {
                        index_url: 'medicine/medicine/table1' + location.search,
                        add_url: 'medicine/medicine/add',
                        edit_url: 'medicine/medicine/edit',
                        del_url: 'medicine/medicine/del_s',
                        multi_url: 'medicine/medicine/multi',
                        import_url: 'medicine/medicine/import',
                        table: 'medicine',
                    },
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    commonSearch: true,
                    searchFormVisible: true,
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                    return ++index;
                                },operate: false},
                            {field: 'code', title: __('产品编码'), operate: 'LIKE'},
                            {field: 'medicine_name', title: __('药品名'), operate: 'LIKE'},
                            {field: 'unit', title: __('单位'), operate: false},
                            {field: 'specification', title: __('规格型号'), operate: false},
                            {field: 'supplier_name', title: __('供应商'), operate: 'LIKE'},
                            {field: 'model_number', title: __('批号'), operate: false},
                            {field: 'type', title: __('药品分类'),searchList: {"药品":__('药品'),"耗材":__('耗材'),"保健品":__('保健品')} },
                            //{field: 'image', title: __('图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                            //{field: 'status', title: __("状态"), searchList: {"0":__('未发布'),"1":__('已发布')}, formatter: Table.api.formatter.status},
                            {field: 'create_time', title: __('创建时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'operate', title: __('Operate'), table: table1,
                                events: Table.api.events.operate,
                                // buttons: [{
                                //     name: 'detail',
                                //     text: __('Detail'),
                                //     icon: 'fa fa-list',
                                //     classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                //     url: 'medicine/medicine/detail'
                                // }],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'medicine/medicine/table2',
                    extend: {
                        index_url: 'medicine/medicine/table2',
                        //add_url: 'oa/log/add',
                        //edit_url: 'oa/log/edit',
                        //del_url: '',
                        multi_url: '',
                        table: 'medicine',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    commonSearch: true,
                    searchFormVisible: true,
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    //searchFormTemplate: 'customformtpl',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                    return ++index;
                                },operate: false},
                            {field: 'code', title: __('产品编码'), operate: 'LIKE'},
                            {field: 'medicine_name', title: __('药品名'), operate: 'LIKE'},
                            {field: 'unit', title: __('单位'), operate: false},
                            {field: 'specification', title: __('规格型号'), operate: false},
                            {field: 'supplier_name', title: __('供应商'), operate: 'LIKE'},
                            {field: 'model_number', title: __('批号'), operate: false},
                            {field: 'type', title: __('药品分类'),searchList: {"药品":__('药品'),"耗材":__('耗材'),"保健品":__('保健品')} },
                            //{field: 'image', title: __('图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                            //{field: 'status', title: __("状态"), searchList: {"0":__('未发布'),"1":__('已发布')}, formatter: Table.api.formatter.status},
                            {field: 'update_time', title: __('创建时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            // {field: 'operate', title: __('Operate'), table: table,
                            //     events: Table.api.events.operate,
                            //     // buttons: [{
                            //     //     name: 'detail',
                            //     //     text: __('Detail'),
                            //     //     icon: 'fa fa-list',
                            //     //     classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                            //     //     url: 'medicine/medicine/detail'
                            //     // }],
                            //     formatter: Table.api.formatter.operate
                            // }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            //table.bootstrapTable('refresh');
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});